package io.modifier.basic.compose.widget.comm

import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.modifier.basic.compose.theme.AppTheme
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.compose.theme.size
import io.modifier.basic.compose.theme.weight
import io.modifier.basic.emo.core.runIf
import io.modifier.basic.emo.ui.core.modifier.throttleClick

@Composable
fun AppBtn(
    text: String,
    modifier: Modifier = Modifier,
    height: Int = 52,
    active: Boolean = true,
    activeTextColor: Color = AppThemeHolder.colors.onAppColor,
    activeBgColor: Color = AppThemeHolder.colors.appColor,
    inactiveTextColor: Color = AppThemeHolder.colors.onGrayContainer,
    inactiveBgColor: Color = AppThemeHolder.colors.grayContainer,
    cornerRadius: Number = 12,
    horPadding: Number = 16,
    onClick: () -> Unit
) {
    val shape = RoundedCornerShape(cornerRadius.toFloat().dp)
    CenterBox(
        modifier
            .runIf(height > 0, { height(height.dp) })
            .runIf(horPadding.toFloat() > 0, { padding(horizontal = horPadding.toFloat().dp) })
            .background(if (active) activeBgColor else inactiveBgColor, shape)
            .clip(shape)
            .throttleClick { onClick() },
    ) {
        Text(
            text = text, style = AppThemeHolder.textStyle.default.size(16).weight(700).copy(
                if (active) activeTextColor else inactiveTextColor
            )
        )
    }
}

@Composable
fun TextBtn(
    text: String,
    modifier: Modifier = Modifier,
    size: Int = 16,
    weight: Int = 400,
    textColor: Color = AppThemeHolder.colors.appColor,
    onClick: () -> Unit
) {
    CenterBox(modifier = modifier.throttleClick { onClick() }) {
        Text(
            text = text,
            style = AppThemeHolder.textStyle.default.size(size).weight(weight).copy(textColor)
        )
    }
}

@Composable
private fun PreviewBtns() {
    AppTheme {
        SpaceColumn {

            AppBtn(text = "AppBtn", modifier = Modifier.width(180.dp)) {
            }

            AppBtn(text = "AppBtn", active = false, modifier = Modifier.width(180.dp)) {
            }


        }
    }
}

@Preview(backgroundColor = 0xFF121212, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Composable
private fun PreviewLightBtns() {
    PreviewBtns()
}

@Preview(backgroundColor = 0xFFf5f5f5, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun PreviewDarkBtns() {
    PreviewBtns()
}